package com.j283_5.wisdom_park.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j283_5.wisdom_park.common.result.ResultData;
import com.j283_5.wisdom_park.mapper.ResinfomanageMapper;
import com.j283_5.wisdom_park.pojo.Resinfomanage;
import com.j283_5.wisdom_park.service.ResinfomanageService;
import com.j283_5.wisdom_park.vo.confcenter_management.ResInfoManageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laoda
 * @since 2022-04-28
 */
@Service
public class ResinfomanageServiceImpl extends ServiceImpl<ResinfomanageMapper, Resinfomanage> implements ResinfomanageService {
    @Resource
    private ResinfomanageMapper resinfomanageMapper;

    @Override
    public ResultData resInfoManage(Page<Resinfomanage> page, Resinfomanage resinfomanage) {
        if (page==null){
            page=new Page<>(1,10);
        }
        QueryWrapper<Resinfomanage> resinfomanageQueryWrapper=new QueryWrapper<>();
        resinfomanageQueryWrapper.eq("re_state",0);
        resinfomanageQueryWrapper.orderByAsc("re_sorrting");
        if (resinfomanage.getReName()!=null){
            resinfomanageQueryWrapper.like("re_name",resinfomanage.getReName());
        }
        if (resinfomanage.getReType()!=null){
            resinfomanageQueryWrapper.eq("re_type",resinfomanage.getReType());
        }
        Page<Resinfomanage> resinfomanagePage=resinfomanageMapper.selectPage(page,resinfomanageQueryWrapper);
        if (resinfomanagePage!=null){
            return ResultData.ok().data(resinfomanagePage);
        }else {
            return ResultData.ok().data("查询失败!");
        }
    }

    @Override
    public ResultData addResInfo(ResInfoManageVo resInfoManageVo) {
        Resinfomanage resinfomanage=new Resinfomanage();
        BeanUtils.copyProperties(resInfoManageVo,resinfomanage);
        int i = resinfomanageMapper.insert(resinfomanage);
        if (i>0){
            return ResultData.ok().message("添加成功!");
        }
        return ResultData.ok().message("添加失败!");
    }

    @Override
    public ResultData deleteResInfo(ArrayList<Integer> reIdlist) {
        UpdateWrapper<Resinfomanage> resinfomanageUpdateWrapper=new UpdateWrapper<>();
        resinfomanageUpdateWrapper.set("re_state",1);
        resinfomanageUpdateWrapper.in("re-id",reIdlist);
        int i=resinfomanageMapper.update(null,resinfomanageUpdateWrapper);
        if(i>0){
            return ResultData.ok().data("删除成功!");
        }
        return ResultData.ok().data("删除失败!");
    }

    @Override
    public ResultData updateResInfo(Resinfomanage resInfoManage) {
        UpdateWrapper<Resinfomanage> resinfomanageUpdateWrapper=new UpdateWrapper<>();
        resinfomanageUpdateWrapper.eq("re_id",0)
                .set("re_name",resInfoManage.getReName())
                .set("re_type",resInfoManage.getReType())
                .set("re_node",resInfoManage.getReNode())
                .set("re_url",resInfoManage.getReUrl())
                .set("re_updatetime",resInfoManage.getReUpdatetime());
        int i = resinfomanageMapper.update(null,resinfomanageUpdateWrapper);
        if (i>0){
            return ResultData.ok().data("修改成功!");
        }else {
            return ResultData.ok().data("修改失败!");
        }
    }
}
